package com.zb.config;

import org.mybatis.spring.SqlSessionFactoryBean;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Import;
import org.springframework.core.io.ClassPathResource;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;

/**
 * @Author: 张波
 * @Time: 2025/8/21 18:52
 */
@Configuration
@MapperScan("com.zb")
@Import({DataSourceConfig.class})
public class SpringConfiguration {
	@Autowired
	private DataSourceConfig dataSourceConfig;
	@Bean("sqlSessionFactory")
	public SqlSessionFactoryBean sqlSessionFactoryBean(){
		SqlSessionFactoryBean sqlSessionFactoryBean = new SqlSessionFactoryBean();
		sqlSessionFactoryBean.setDataSource(dataSourceConfig.getMyConnection());
		sqlSessionFactoryBean.setConfigLocation(new ClassPathResource("mybatis.xml"));
		return sqlSessionFactoryBean;
	}
	@Bean("transactionManager")
	public DataSourceTransactionManager dataSourceTransactionManager(){
		DataSourceTransactionManager transactionManager=new DataSourceTransactionManager();
		transactionManager.setDataSource(dataSourceConfig.getMyConnection());
		return transactionManager;
	}
}
